<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="1200"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{'top':'93px','left':'154px'}"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="取消"
      okText="保存"
      style="top:10%;height: 80%;">
      <template slot="footer">
        <a-button key="back" v-if="isReadOnly" @click="handleCancel">
          取消
        </a-button>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="vendorModal">
          <a-row class="form-row" :gutter="24">
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="oprType === 'sale' ? '销售对账单' : '采购对账单'">
                <a-input placeholder="请输入" v-decorator.trim="[ 'statementType', validatorRules.statementType]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="统计类型">
                <a-input placeholder="请输入" v-decorator.trim="[ 'statisticalType' ]" />
              </a-form-item>
            </a-col>
          </a-row>
          <a-row class="form-row" :gutter="24">
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工厂">
                <a-input placeholder="请输入" v-decorator.trim="[ 'factory' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="oprType === 'sale' ? '客户编号' : '供应商编号'">
                <a-input placeholder="请输入" v-decorator.trim="[ 'supplierNumber' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="oprType === 'sale' ? '客户名称': '供应商名称'">
                <a-input placeholder="请输入" v-decorator.trim="[ 'supplierName' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="订单编号">
                <a-input placeholder="请输入" v-decorator.trim="[ 'depotNumber' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="行项目">
                <a-input placeholder="请输入" v-decorator.trim="[ 'lineProject' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="期末应付">
                <a-input v-decorator.trim="[ 'barCode' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="物料描述">
                <a-input placeholder="请输入" v-decorator.trim="[ 'materials' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="FYrRef">
                <a-input-number style="width:100%" placeholder="请输入" v-decorator.trim="[ 'fyrRef' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="参考凭证">
                <a-input placeholder="请输入" v-decorator.trim="[ 'referenceCertificate' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="RfIt">
                <a-input placeholder="请输入" v-decorator.trim="[ 'rfIt' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="入库数量">
                <a-input placeholder="请输入" v-decorator.trim="[ 'materialCount' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="仍要开票数量">
                <a-input placeholder="请输入" v-decorator.trim="[ 'invoiceNum' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="订单单位">
                <a-input placeholder="请输入" v-decorator.trim="[ 'materialUnit' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="oprType === 'sale' ? '销售人员' : '采购人员'">
                <a-input placeholder="请输入" v-decorator.trim="[ 'orderPeople' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="不含税单价(本币)">
                <a-input placeholder="请输入" v-decorator.trim="[ 'unitPrice' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="仍要开票的净价值(本币)">
                <a-input placeholder="请输入" v-decorator.trim="[ 'needInvoice' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="仍要开票价值（含税）">
                <a-input placeholder="请输入" v-decorator.trim="[ 'needInvoiceTax' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="货币单位 默认RMB">
                <a-input placeholder="请输入" v-decorator.trim="[ 'currencyType' ]" />
              </a-form-item>
            </a-col>
            <a-col :span="24/2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="租户ID">
                <a-input placeholder="请输入" v-decorator.trim="[ 'tenantId' ]" />
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import pick from 'lodash.pick'
import { addAccountCheckInfo, editAccountCheckInfo, checkSupplier } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
import { mixinDevice } from '@/utils/mixin'
export default {
    name: 'AccountModal',
    props: {
        oprType: {
            type: String,
            default: '',
            required: false
        }
    },
    mixins: [mixinDevice],
    data () {
        return {
            title: '操作',
            visible: false,
            model: {},
            isReadOnly: false,
            labelCol: {
                xs: { span: 28 },
                sm: { span: 4 }
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 20 }
            },
            confirmLoading: false,
            form: this.$form.createForm(this),
            validatorRules: {}
        }
    },
    created () {
    },
    methods: {
        add () {
            this.edit({})
        },
        edit (record) {
            this.form.resetFields()
            this.model = Object.assign({}, record)
            this.visible = true
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'statementType', 'statisticalType', 'factory', 'supplierName', 'supplierNumber', 'depotNumber', 'beginNeedPay', 'allNeedPay', 'materials', 'fyrRef',
                    'referenceCertificate', 'rfIt', 'materialCount', 'invoiceNum', 'materialUnit', 'orderPeople', 'unitPrice', 'needInvoice', 'needInvoiceTax', 'currencyType', 'tenantId'))
                // autoJumpNextInput('AccountModal')
            })
        },
        close () {
            this.$emit('close')
            this.visible = false
        },
        handleOk () {
            const that = this
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let formData = Object.assign(this.model, values)
                    formData.type = '供应商'
                    let obj
                    if (!this.model.id) {
                        obj = addAccountCheckInfo(formData)
                    } else {
                        obj = editAccountCheckInfo(formData)
                    }
                    obj.then((res) => {
                        if (res.code === 200) {
                            that.$emit('ok')
                        } else {
                            that.$message.warning(res.data.message)
                        }
                    }).finally(() => {
                        that.confirmLoading = false
                        that.close()
                    })
                }
            })
        },
        handleCancel () {
            this.close()
        },
        validateSupplierName (rule, value, callback) {
            let params = {
                name: value,
                type: '供应商',
                id: this.model.id ? this.model.id : 0
            }
            checkSupplier(params).then((res) => {
                if (res && res.code === 200) {
                    if (!res.data.status) {
                        callback()
                    } else {
                        // callback('名称已经存在')
                    }
                } else {
                    callback(res.data)
                }
            })
        }
    }
}
</script>
<style scoped>

</style>
